package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuSettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/8/5 10:52
 * @Description:
 */
@RestController
@RequestMapping("/system/setting")
public class MenuSettingController {

	@Reference
    MenuSettingService systemSettingService;
	/**
	 *  @描述：增加菜单项
	 */
	@RequestMapping("/addMenu")
	public Result addMenu(@RequestBody Menu menu) {
		try {
			systemSettingService.addMenu(menu);
			return new Result(true,"增加菜单成功");
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, "增加菜单失败");
		}

	}
	/**
	 *  @描述：增加菜单项
	 */
	@RequestMapping("/edit")
	public Result edit(@RequestBody Menu menu) {
		try {
			systemSettingService.edit(menu);
			return new Result(true,"编辑菜单成功");
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, "编辑菜单失败");
		}

	}
	/**
	 *  @描述：分页查询
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
		return systemSettingService.findPage(queryPageBean.getCurrentPage(),
				queryPageBean.getPageSize(),
				queryPageBean.getQueryString());
	}
	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/findParentMenu")
	public Result findById() {
		try {
			List<Menu> listMenus =  systemSettingService.findParentMenu();
			return new Result(true, "成功",listMenus);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, "失败");

		}
	}
	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/findMenuByID")
	public Result findMenuByID(Integer id) {
		try {
			Menu menu =  systemSettingService.findMenuByID(id);
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,menu);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

		}
	}
	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/findAll")
	public Result findAll() {
		try {
			List<Menu> menus  =  systemSettingService.findAll();
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,menus);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

		}
	}
	/**
	 *  @描述：根据id查询基本信息
	 */
	@RequestMapping("/delete")
	public Result delete(@RequestBody Menu menu) {
		try {
			systemSettingService.delete(menu);
			return new Result(true, "删除成功");
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, "删除失败");

		}
	}
}
